<template>
  <el-card class="common-container">
    <div style="margin-bottom: 20px; font-size: 20px;color: #606266;">用户管理</div>
    <div class="search-container">
      <el-input class="search-wrap" size="medium" />
      <el-button type="primary" size="medium" icon="search">查询</el-button>
      <el-button type="primary" size="medium" icon="add" @click="handleAdd">新建用户</el-button>
    </div>

    <el-table size="medium" :data="list" border>
      <el-table-column prop="username" align="center" label="登录名" />
      <el-table-column prop="name" align="center" label="用户昵称" />
      <el-table-column prop="roles" align="center" label="用户角色">
        <template slot-scope="scope">
          <span>{{ toRolesName(scope.row.roles) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" align="center" label="用户状态">
        <template slot-scope="scope">
          <span>{{ statusOptions[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" align="center" label="创建时间" />

      <el-table-column align="center" label="操作" width="350">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="primary" @click="handleResetPassword(scope.row)">重置密码</el-button>

          <!--<el-button v-if="scope.row.status === 1" size="small" type="danger">停用</el-button>-->
          <!--<el-button v-if="scope.row.status === 0" size="small" type="primary">启用</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :total="total"
        layout="total, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <user-dialog ref="userDialog" :scope="scope" />
    <reset-pw ref="resetPw" :scope="scope" />
  </el-card>
</template>

<script>
import { listUser } from '@/api/user'
import UserDialog from './components/user'
import ResetPw from './components/resetPw'

export default {
  components: {
    UserDialog, ResetPw
  },
  data() {
    return {
      scope: 0,
      total: 0,
      statusOptions: ['停用', '正常'],
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        name: undefined
      },
      list: []
    }
  },

  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      listUser(this.listQuery).then(res => {
        if (res.code === 0) {
          this.list = res.data
          this.total = res.data.length
        }
      })
    },
    toRolesName(roles) {
      const rolesNames = []
      for (let i = 0; i < roles.length; i++) {
        rolesNames.push(roles[i].name)
      }
      return rolesNames.join(',')
    },
    handleAdd() {
      this.$refs.userDialog.openAdd().then(() => {
        this.fetchData()
      })
    },
    handleEdit(user) {
      this.$refs.userDialog.openEdit(user.id).then(() => {
        this.fetchData()
      })
    },

    handleFilter() {
      this.fetchData()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val
      this.fetchData()
    },
    handleResetPassword(user) {
      this.$refs.resetPw.open(user.id)
    }

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .search-container {
    margin-top: 20px;
    margin-bottom: 20px
  }

  .search-wrap {
    width: 300px;
    margin-right: 10px
  }
</style>
